﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace ncp.Business
{
    public class MessageInfoBll
    {
        /// <summary>
        /// 得到留言信息表
        /// </summary>
        public static DataTable GetMessageInfoTable(string p_Where, string p_Orderby, string pDataBase)
        {
            string sqlStr = "select * from bi_MessageInfo";
            if (!string.IsNullOrEmpty(p_Where))
                sqlStr += " where " + p_Where;
            if (!string.IsNullOrEmpty(p_Orderby))
                sqlStr += " order by " + p_Orderby;
            return CommonBll.GetdtList(sqlStr, pDataBase);
        }

        /// <summary>
        /// 得到留言记录数
        /// </summary>
        public static int GetMessageInfoCount(string pcWhere, string pDataBase)
        {
            string sqlStr = "select count(*) from bi_SendMessage_v";
            if (!string.IsNullOrEmpty(pcWhere))
                sqlStr += " where " + pcWhere;
            return CommonBll.ReadInteger(sqlStr, pDataBase);
        }

        public static DataTable GetMessageInfoTable(int piPageIndex, int piPageSize, string pcFieldList, string pcOrderBy, Dictionary<string, object> dicWhere, string pDataBase)
        {
            List<SqlParameter> ilist = new List<SqlParameter>();
            string pcWhere = "1=1 ";
            foreach (KeyValuePair<string, object> kvp in dicWhere)
            {
                pcWhere += " and " + kvp.Key + "=@" + kvp.Key;
                ilist.Add(new SqlParameter("@" + kvp.Key, kvp.Value));
            }
            string sqlStr = CommonBll.GetPagingSQL(piPageIndex, piPageSize, "bi_SendMessage_v", pcFieldList, "Message_Id", pcOrderBy, pcWhere);
            SqlParameter[] paras = ilist.ToArray();
            return CommonBll.GetdtList(sqlStr, pDataBase, paras);
        }


        /// <summary>
        /// 修改留言信息
        /// </summary>
        public static int UpdateMessageInfo(Dictionary<string, object> dicMessage,Dictionary<string, object> dicWhere, string pDataBase)
        {
            List<SqlParameter> ilist = new List<SqlParameter>();
            string field = "", where = "";
            if (dicMessage.Count == 0)
            {
                return 0;
            }
            foreach (KeyValuePair<string, object> kvp in dicMessage)
            {
                field += "," + kvp.Key + "=@" + kvp.Key;
                ilist.Add(new SqlParameter("@" + kvp.Key, kvp.Value));
            }
            foreach (KeyValuePair<string, object> kvp in dicWhere)
            {
                where += " and " + kvp.Key + "=@" + kvp.Key;
                ilist.Add(new SqlParameter("@" + kvp.Key, kvp.Value));
            }
            SqlParameter[] paras = ilist.ToArray();
            string sqlStr = "UPDATE dbo.bi_MessageInfo set " + field.Substring(1) + " where 1=1 " + where;
            return CommonBll.ExecuteNonQuery(CommandType.Text, sqlStr, pDataBase, paras);
        }

        /// <summary>
        /// 增加留言信息
        /// </summary>
        public static int InsertMessageInfo(Dictionary<string, object> dicMessage, string pDataBase)
        {
            List<SqlParameter> ilist = new List<SqlParameter>();
            string field = "",values="";
            if (dicMessage.Count==0)
            {
                return 0;
            }
            foreach (KeyValuePair<string, object> kvp in dicMessage)
            {
                field += "," + kvp.Key;
                values += ",@" + kvp.Key;
                ilist.Add(new SqlParameter("@" + kvp.Key, kvp.Value));
            }
            SqlParameter[] paras = ilist.ToArray();
            string sqlStr = "insert into dbo.bi_MessageInfo (" + field.Substring(1) + ") values (" + values.Substring(1) + ")";
            return CommonBll.ExecuteNonQuery(CommandType.Text, sqlStr, pDataBase, paras);
        }

        /// <summary>
        /// 删除留言
        /// </summary>
        public static int DeleteMessageInfo(string Message_Id, string pDataBase)
        {
            string sqlStr = "delete from bi_MessageInfo where Message_Id in(" + Message_Id + ")";
            return CommonBll.ExecuteNonQuery(sqlStr, pDataBase);
        }

        /// <summary>
        /// 删除留言
        /// </summary>
        public static int DeleteMessageInfo(string Message_Id,string clientId, string pDataBase)
        {
            string sqlStr = "update bi_MessageInfo set Message_Status=0 where Message_Id =@Message_Id and Client_Id=@Client_Id";
            SqlParameter[] paras = new SqlParameter[]
                                       {
                                           new SqlParameter("@Message_Id", Message_Id),
                                           new SqlParameter("@Client_Id", clientId)
                                       };
            return CommonBll.ExecuteNonQuery(CommandType.Text, sqlStr, pDataBase, paras);
        }
    }
}
